<?php
/* 
* @Title:  [订单控制器,包括购物车/订单]
* @Author: {chenlei} [chenleib5@126.com]
* @Date:   2014-04-14 15:17:51
* @Last Modified by:   Administrator
* @Last Modified time: 2014-04-29 10:10:00
* @Copyright:  [hn7m.com]
*/
class OrderControl extends CommonControl{
    private $orderModel=NULL;
    private $listModel=NULL;

    /**
     * [__init 自动加载模型类]
     * @return [type] [description]
     */
    public function __init(){
        parent::__init();
        $this->orderModel=K('Order');
        $this->listModel=K('Orderlist');
    }
	/**
	 * [cart 购物车页]
	 * @return [type] [description]
	 */
	public function cart(){
		$this->display();
	}
    /**
     * [delOne 删除一条购物车商品]
     * @return [type] [description]
     */
    public function del_one_cart(){
        if(IS_AJAX){
            $session_key = Q('session_key');
            if(!isset($session_key)){
                die('非法操作');
            };
            cart::del($session_key);
            //如果是登陆状态,改变session的同时改变数据库cart表
            if(isset($_SESSION['uid'])&&isset($_SESSION['username'])){
                $uid=$_SESSION['uid'];
                $this->session_to_cart($uid);  //此方法写在通用控制器中;
            }
            if(empty($_SESSION['cart']['goods'])){
                unset($_SESSION['cart']);
            }
            if(!isset($_SESSION['cart']['goods'][$session_key])){
                $return=array(
                    'state'     =>1,
                    'message'   =>'删除成功!',
                    'timeout'   =>2
                );
            }else{
               $return=array(
                    'state'     =>0,
                    'message'   =>'删除失败,可能是权限不够!',
                    'timeout'   =>2
                ); 
            }
            $this->ajax($return);
        }
    }
    /**
     * [del_some_cart 删除多条选中的购物车商品]
     * @return [type] [description]
     */
    public function del_some_cart(){
        if(IS_AJAX){
            $session_keys=Q('post.$session_keys');
            foreach ($session_keys as $key => $value) {
                if(!isset($value)){
                    die('非法操作');
                }
                cart::del($value);
            }
            //如果是登陆状态,改变session的同时改变数据库cart表
            if(isset($_SESSION['uid'])&&isset($_SESSION['username'])){
                $uid=$_SESSION['uid'];
                $this->session_to_cart($uid);  //此方法写在通用控制器中;
            }
            if(empty($_SESSION['cart']['goods'])){
                unset($_SESSION['cart']);
            }
            $return=array(
                'state'     =>1,
                'message'   =>'删除成功!',
                'timeout'   =>3
                );
            $this->ajax($return);
        }
    }
    /**
     * [ajax_dec_num 异步减少购物车商品数量]
     * @return [type] [description]
     */
    public function ajax_dec_num(){
        $data=array(
            'sid'   =>Q('post.sid'),// 唯一 sid，添加购物车时自动生成
            'num'   =>Q('post.num')
        );
        Cart::update($data);
        //如果是登陆状态,改变session的同时改变数据库cart表
        if(isset($_SESSION['uid'])&&isset($_SESSION['username'])){
            $uid=$_SESSION['uid'];
            $this->session_to_cart($uid);  //此方法写在通用控制器中;
        }
        $this->ajax($_SESSION);
    }
    /**
     * [order 订单页面]
     * @return [type] [description]
     */
    public function order(){
        $uid=Q('session.uid',0,'intval');
        $data=K('UserAddress')->get_last_data($uid);//获得最后的一条数据
        $address=$data['raddress'].' ( '. $data['rconsignee'] .' 收 ) '. $data['rphone_cnee'] . ' 邮政编码：'.$data['rzipcode'];

        $this->assign('address',$address);//分配地址
        $this->assign('address_data',$data);//分配其他数据
        
        $this->display();  
    }
    /**
     * [ajax_add_address 异步添加新地址]
     * @return [type] [description]
     */
    public function ajax_add_address(){
        if (IS_AJAX) {
            //前台JS验证,后台PHP验证
            if(!$this->orderModel->create()){
                $return=array(
                    'state'     =>0,
                    'message'   =>$this->orderModel->error
                    );
                echo json_encode($return); die;
            }

            //组合数据添加到数据库order表
            $ophone_cnee=Q('post.phone_cnee',0);
            $ophone_cnee=($ophone_cnee==0)?Q('post.phone_buyer'):$ophone_cnee;
            $uid=Q('session.uid');
            $save_address=Q('save_address',0,'intval');
            $address=Q('post.address').' ( '. Q('post.consignee') .' 收 ) '. $ophone_cnee . ' 邮政编码：'.Q('post.zipcode');

            //如果用户选中保存地址,则将地址保存到用户地址表中
            if($save_address==1){
                $address_data=array(
                    'raddress'      =>Q('post.address'),
                    'qm_user_uid'   =>$uid,
                    'rconsignee'    =>Q('post.consignee'),
                    'rzipcode'      =>Q('post.zipcode'),
                    'rphone_cnee'   =>Q('post.phone_cnee'),
                    'rphone_buyer'  =>Q('post.phone_buyer')
                );
                if(!K('UserAddress')->add_data($address_data)){
                    $return=array(
                        'state'     =>0,
                        'message'   =>'地址保存成功,但未能保存到您的收货地址列表中'
                        );
                    echo json_encode($return); die;
                };
            }

            $return=array(
                'state'         =>1,
                'address'       =>$address,
                'oconsignee'    =>Q('post.consignee'),
                'oaddress'      =>Q('post.address'),
                'ozipcode'      =>Q('post.zipcode'),
                'ophone_cnee'   =>Q('post.phone_cnee'),
                'ophone_buyer'  =>Q('post.phone_buyer')
                );
            echo json_encode($return); die;
        }
    }
    /**
     * [go_payment 提交订单,成功后跳转到付款页面]
     * @return [type] [description]
     */
    public function go_payment(){
        if(IS_POST){
            //组合数据压入到数据库order表
            $data=array(
                'oconsignee'    =>Q('post.oconsignee'),
                'oaddress'      =>Q('post.oaddress'),
                'ozipcode'      =>Q('post.ozipcode'),
                'ophone_cnee'   =>Q('post.ophone_cnee'),
                'ophone_buyer'  =>Q('post.ophone_buyer'),
                'ototal'        =>Q('post.ototal'),
                'oorder_time'   =>time(),
                'qm_user_uid'   =>Q('session.uid')
                );
            $oid=$this->orderModel->add_data($data);
            //组合数据压到数据库order_list表
            foreach ($_POST['lgoods'] as $key => $value) {
                $list_data=array(
                    'lnumber'=>$value['lnumber'],
                    'lprice'=>$value['lprice'],
                    'lmoney'=>$value['lmoney'],
                    'qm_combine_bid'=>$value['qm_combine_bid'],
                    'qm_goods_gid'=>$value['qm_goods_gid'],
                    'qm_order_oid'=>$oid
                );
                $this->listModel->add_data($list_data);
            }
        }
        $uid=Q('session.uid');
        session('cart',NULL);//提交订单后,就清除掉购物车的数据;这个地方是和cancle_order()是一个互逆的过程;
        $this->session_to_cart($uid);
        go(U('payment',array('oid'=>$oid)));
    }

    public function payment(){
        $uid=Q('session.uid');
        $oid=Q('get.oid',0,'intval');
        $this->assign('oid',$oid);//分配oid用于隐藏域便于取消订单和付款操作

        $total=$this->orderModel->get_one_value(array('oid'=>$oid),'ototal');
        $this->assign('total',$total);//分配总金额;

        $order=$this->listModel->get_data(array('qm_order_oid'=>$oid));
        foreach ($order as $key => $value) {
            $order[$key]['imgUrl']=K('Goods')->get_onedate_value($value['qm_goods_gid'],'gthumb_img');
            $order[$key]['name']=K('Goods')->get_onedate_value($value['qm_goods_gid'],'gname');
            $order[$key]['cid']=K('Goods')->get_onedate_value($value['qm_goods_gid'],'qm_category_cid');
        }
        foreach ($order as $key => $value) {
           $order[$key]['cate']=K('Category')->get_onedate_value($value['cid'],'cname');
        }

        $this->assign('order',$order);//分配订单列表;
        if(IS_POST){
            //订单状态变成2,已付款
            //库存减少
            //清除$_SESSION['cart'];
            //修改购物车表
            $oid=Q('post.oid',0,'intval');
            $this->orderModel->update_data(array('oid'=>$oid),array('ostate'=>2));

            if(isset($_SESSION['cart']['goods'])){
                foreach ($_SESSION['cart']['goods'] as $key => $value) {
                    $bid=$value['bid'];
                    $kucun=K('Combine')->get_onedate_value($bid,'binventory');
                    $num=$value['num'];
                    $dec=$kucun-$num;
                    $data=array('binventory'=>$dec);
                    K('Combine')->save_data($bid,$data);
                }
                session('cart',NULL);//清除购物车session;
                $this->session_to_cart($uid);
            }

            go(U('paysuccess'));

        }
        $this->display();
    }
    /**
     * [cancle_order 取消订单]
     * @return [type] [description]
     */
    public function cancle_order(){
        $oid=Q('get.oid',0,'intval');
        $uid=Q('get.uid',0,'intval');
        $this->orderModel->del_data(array('oid'=>$oid));
        $this->listModel->del_data(array('qm_order_oid'=>$oid));
        $this->cart_to_session($uid);//重新制造购物车session,这个地方是和go_payment()方法中互逆的过程
        go(U('order'));
    }
    /**
     * [paysuccess 付款成功页面]
     * @return [type] [description]
     */
    public function paysuccess(){
        $this->display();
    }
}
?>